'''
Created on 10.06.2013

@author: Cornelius
'''
from libavg import avg

class LobbyScreen(avg.DivNode):
    '''
    classdocs
    '''
    __timer = "60"
    issetPlayer = [0,0,0,0]
    playerCount = 0
    
    #initialize lobbyScrren(all Nodes)
    def __init__(self, size, parent):
        '''
        Constructor
        '''
        super(LobbyScreen, self).__init__(size=size)
        self.registerInstance(self, parent)
        '''self.__node = avg.DivNode(size=sizeparam, parent=parentparam) '''
        self.rect1 = avg.RectNode(pos=(0,0),size=size,  fillopacity=1,
                                     fillcolor="FFFFFF",  color="FFFFFF", parent=self)
        self.backgroundnode = avg.ImageNode(id="background2", href="images/background_horizontal.png", pos=(size[0]/2,0), parent=self, size=(size[0]/2,size[1]/3))
        #qr-Code div
        self.bombdiv = avg.DivNode(parent = self, pos=(size[0]-size[0]*7/24,size[1]-size[1]/3), size=(size[0]/3,size[1]/4))
        self.bombnode = avg.ImageNode(id="bomb2", href="images/bomb.png", pos=(0,size[1]/15), parent=self.bombdiv, size=(size[1]/4,size[1]/4) )
        self.qrnode = avg.ImageNode(id="qr2", href="images/qrcode.png", pos=(size[0]/3/2-size[0]/40,size[1]/15), parent=self.bombdiv, size=(size[1]/4,size[1]/4) )
        self.textnode3 = avg.WordsNode(pos=(size[0]/6-20,0), alignment="center", font="calibri", variant="", text="Spiel Beitreten!", color="000000", fontsize=size[1]/20, parent=self.bombdiv)
        
        #self.bombnode = avg.ImageNode(id="bomb2", href="images/bomb.png", pos=(size[0]*1/8,size[1]*2/5), parent=self, size=(size[1]/2,size[1]/2) )
        self.textnode = avg.WordsNode(pos=(size[0]*1/16,size[1]*1/8),font="calibri", variant="bold", text="Bomberman", color="000000", fontsize=size[1]/10, parent=self)
        self.textnode2 = avg.WordsNode(pos=(self.textnode.pos[0]+size[0]*1/32,self.textnode.pos[1]+size[1]/10),font="calibri", variant="", text="Es wird auf Spieler gewartet...", color="000000", fontsize=size[1]/20, parent=self)
        self.timer = avg.WordsNode(pos=(size[0]*1/16,size[1]*7/8),font="calibri", variant="bold", text=self.__timer, color="000000", fontsize=size[1]/10, parent=self)
        
        #player div
        self.playerdiv = avg.DivNode(parent=self, pos=(0,size[1]/3), size=(size[0]/2,size[1]/2))
        #self.rect2 = avg.RectNode(pos=(0,0),fillopacity=1,fillcolor="000000",  color="000000", parent=self.playerdiv,size=(size[0]/2,size[1]/2))
        #self.p1 = avg.RectNode(pos=(0,0),fillopacity=1,fillcolor="FFFFFF",  color="000000", parent=self.playerdiv,size=(size[0]/2/2,size[1]/2/2))
        #self.p3 = avg.RectNode(pos=(0,size[1]/2/2),fillopacity=1,fillcolor="FFFFFF",  color="000000", parent=self.playerdiv,size=(size[0]/2/2,size[1]/2/2))
        #self.p2 = avg.RectNode(pos=(size[0]/2/2,0),fillopacity=1,fillcolor="FFFFFF",  color="000000", parent=self.playerdiv,size=(size[0]/2/2,size[1]/2/2))
        #self.p4 = avg.RectNode(pos=(size[0]/2/2,size[1]/2/2),fillopacity=1,fillcolor="FFFFFF",  color="000000", parent=self.playerdiv,size=(size[0]/2/2,size[1]/2/2))
        
        self.player1 = avg.DivNode(pos=(0,0), parent=self.playerdiv,size=(size[0]/2/2,size[1]/2/2))
        self.player3 = avg.DivNode(pos=(0,size[1]/2/2), parent=self.playerdiv,size=(size[0]/2/2,size[1]/2/2))
        self.player2 = avg.DivNode(pos=(size[0]/2/2,0), parent=self.playerdiv,size=(size[0]/2/2,size[1]/2/2))
        self.player4 = avg.DivNode(pos=(size[0]/2/2,size[1]/2/2), parent=self.playerdiv,size=(size[0]/2/2,size[1]/2/2))
        
        self.con1 = avg.DivNode(pos=(0,0), parent=self.player1, size=(size[0]/2/2,size[1]/2/2))
        self.con2 = avg.DivNode(pos=(0,0), parent=self.player2, size=(size[0]/2/2,size[1]/2/2))
        self.con3 = avg.DivNode(pos=(0,0), parent=self.player3, size=(size[0]/2/2,size[1]/2/2))
        self.con4 = avg.DivNode(pos=(0,0), parent=self.player4, size=(size[0]/2/2,size[1]/2/2))
        
    #reset value of gameStart countdown    
    def resetTimer(self):
        self.timer.text = self.__timer
    
    #adds a new player to lobbyScreen    
    def addPlayer(self,player):
        if(player.getName() == None):
            return
        self.playerCount +=1
        name = player.getName()
        charID = player.getCharID()
        counter = 1
        for i in self.issetPlayer:
            if(i==0):
                if(counter ==1):
                    avg.ImageNode(pos=(0,0),parent=self.con1, href="images/char0"+str(charID)+"/"+str(self.playerCount)+".png", size =(self.size[0]/2/2/2,self.size[0]/2/2/2))
                    avg.WordsNode(pos=(self.size[0]/2/2/2,0), size=(self.size[0]/2/2/2,self.size[0]/2/2/2), parent=self.con1, font="calibri", variant="", text=name, color="000000", fontsize=self.size[1]/35)
                if(counter ==2):
                    avg.ImageNode(pos=(0,0),parent=self.con2, href="images/char0"+str(charID)+"/"+str(self.playerCount)+".png", size =(self.size[0]/2/2/2,self.size[0]/2/2/2))
                    avg.WordsNode(pos=(self.size[0]/2/2/2,0), size=(self.size[0]/2/2/2,self.size[0]/2/2/2), parent=self.con2, font="calibri", variant="", text=name, color="000000", fontsize=self.size[1]/35)
                if(counter ==3):
                    avg.ImageNode(pos=(0,0),parent=self.con3, href="images/char0"+str(charID)+"/"+str(self.playerCount)+".png", size =(self.size[0]/2/2/2,self.size[0]/2/2/2))
                    avg.WordsNode(pos=(self.size[0]/2/2/2,0), size=(self.size[0]/2/2/2,self.size[0]/2/2/2), parent=self.con3, font="calibri", variant="", text=name, color="000000", fontsize=self.size[1]/35)
                if(counter ==4):
                    avg.ImageNode(pos=(0,0),parent=self.con4, href="images/char0"+str(charID)+"/"+str(self.playerCount)+".png", size =(self.size[0]/2/2/2,self.size[0]/2/2/2))
                    avg.WordsNode(pos=(self.size[0]/2/2/2,0), size=(self.size[0]/2/2/2,self.size[0]/2/2/2), parent=self.con4, font="calibri", variant="", text=name, color="000000", fontsize=self.size[1]/35)
                self.issetPlayer[counter-1] =1
                return
            else:
                counter +=1
    
    #updates all players: remove them and then add them    
    def updatePlayers(self,players):
        while(self.con1.getNumChildren()>0):
            self.con1.removeChild(self.con1.getChild(0))
        while(self.con2.getNumChildren()>0):
            self.con2.removeChild(self.con2.getChild(0))
        while(self.con3.getNumChildren()>0):
            self.con3.removeChild(self.con3.getChild(0))
        while(self.con4.getNumChildren()>0):
            self.con4.removeChild(self.con4.getChild(0))
        
        self.issetPlayer = [0,0,0,0]
        self.playerCount = 0
            
        for p in players.itervalues():
            self.addPlayer(p)
            
        